﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BeniBastanYaratWinFormProject
{
    public partial class SiparisOzelRapor : Form
    {

        dbbaglantiDataContext db = new dbbaglantiDataContext();
        public SiparisOzelRapor()
        {
            InitializeComponent();
        }

        private void OzelRapor_Load(object sender, EventArgs e)
        {
            var teslimatTuru = (from x in db.Siparislers select x.SiparisDurumu).Distinct().ToList();
            teslimatTuru.Insert(0, "-");
            cmbSiparisDurumu.DataSource = teslimatTuru;

            TabloyuDoldur(false);
        }

        
        private void TabloyuDoldur(bool TeslimatDikkateAl)
        {

            DateTime bitisnext = dtpBitis.Value.AddDays(1);

            DateTime baslangictarihi = new DateTime(dtpBaslangic.Value.Year, dtpBaslangic.Value.Month, dtpBaslangic.Value.Day, 0, 0, 0);
            DateTime bitiştarihi = new DateTime(bitisnext.Year, bitisnext.Month, bitisnext.Day, 0, 0, 0);

            if (TeslimatDikkateAl)
            {


                var sorguTurHaric = (from x in db.Siparislers
                                     where (x.SiparisTarihi >= baslangictarihi && x.SiparisTarihi <= bitiştarihi) && x.SiparisDurumu == cmbSiparisDurumu.SelectedItem.ToString()
                                     join y in db.UyeDetaylars on x.UserID equals y.UyeId into adim1
                                     from k in adim1.DefaultIfEmpty()
                                     join z in db.SiparisDetaylaris on x.SiparisID equals z.SiparisID into adim2
                                     from son in adim2

                                     select new
                                     {
                                         k.UyeAdi,
                                         k.UyeSoyadi,
                                         x.SiparisID,
                                         x.SiparisTarihi,
                                         x.SiparisDurumu,
                                         son.Fiyat
                                     });




                gridRapor.DataSource = sorguTurHaric;

                decimal toplam = 0;
                foreach (var item in sorguTurHaric)
                {
                    toplam += item.Fiyat;
                }
                lblToplamSatis.Text = sorguTurHaric.Count().ToString();
                lblCiro.Text = toplam.ToString();
            }
            else
            {
                var sorguTurHaric = (from x in db.Siparislers

                                     join y in db.UyeDetaylars on x.UserID equals y.UyeId into adim1
                                     from k in adim1.DefaultIfEmpty()
                                     join z in db.SiparisDetaylaris on x.SiparisID equals z.SiparisID into adim2
                                     from son in adim2
                                     where (x.SiparisTarihi >= baslangictarihi && x.SiparisTarihi <= bitiştarihi)

                                     select new
                                     {
                                         k.UyeAdi,
                                         k.UyeSoyadi,
                                         x.SiparisID,
                                         x.SiparisTarihi,
                                         x.SiparisDurumu,
                                         son.Fiyat
                                     });

                gridRapor.DataSource = sorguTurHaric;

                decimal toplam = 0;
                foreach (var item in sorguTurHaric)
                {
                    toplam += item.Fiyat;
                }
                lblToplamSatis.Text = sorguTurHaric.Count().ToString();
                lblCiro.Text = toplam.ToString();

            }



        }

        private void btnFiltrele_Click(object sender, EventArgs e)
        {
            if (cmbSiparisDurumu.SelectedItem.ToString() != "-")
            {
                TabloyuDoldur(true);


            }
            else
            {
                TabloyuDoldur(false);
            }
        }


    }
}
